<template>
  <div class="user">
    <div v-if="buyList.length>0">
      <div class="goodList">
        <UserList v-for="item in buyList" :key="item.id" :id="item.id" :title="item.title" :price="item.price" :src="item.src" @getDel="del"></UserList>
      </div>
      <div>
        <span>商品总价:</span>
        <span>${{ total }}</span>
      </div>
    </div>
    <div v-else>
      <p>暂无产品</p>
    </div>

    <div class="navBar">
      <nav>
        <router-link to="/home">购物车</router-link>
        <router-link to="/user" class="active">我的</router-link>
      </nav>
    </div>
  </div>
</template>

<script>
import bus from '../eventBus.js'

import UserList from '@/components/userList.vue'

export default {
  name: 'UserView',
  data() {
    return {
      goodId: 0,
      buyList: []
    }
  },
  methods: {
    del(id) {
      this.buyList.forEach((x, index) => {
        if (x.id === id) {
          this.buyList.splice(index, 1)
        }
      })
    }
  },
  mounted() {
    bus.$on('share', val => {
      this.buyList.push({
        id: this.goodId,
        title: val.title,
        price: val.price,
        src: val.src
      })
      this.goodId++
    })
  },
  computed: {
    total() {
      return this.buyList.reduce((x, item) => (x += item.price), 0)
    }
  },
  components: {
    UserList
  }
}
</script>

<style scoped>
.navBar {
  width: 100%;
  position: fixed;
  bottom: 0;
}
nav {
  margin: 0 5px;
  background-color: #fff;
  border-top: 1px solid #000;
  display: flex;
  flex-direction: row;
}
nav a {
  width: 100%;
  text-align: center;
  color: #f18741;
  text-decoration: none;
}
.active {
  background-color: #fef5ef;
}
</style>